package great.project.jpa.sign.table.domain.entity;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonInclude;
import great.project.jpa.sign.table.dto.RoleDto;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.ToString;
import org.hibernate.annotations.NamedNativeQueries;
import org.hibernate.annotations.NamedNativeQuery;
import org.springframework.format.annotation.DateTimeFormat;

import javax.persistence.ColumnResult;
import javax.persistence.ConstructorResult;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.SqlResultSetMapping;
import javax.persistence.SqlResultSetMappings;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import java.util.Date;


/**
    * 角色表
 * @author v_zhangshun01
 */
@Getter
@Setter
@Builder
@ToString
@AllArgsConstructor
@NoArgsConstructor
@JsonInclude(JsonInclude.Include.NON_NULL)
@Table
@Entity
@NamedQueries({@NamedQuery(
        name = "SysRole.selectRoleNameForNamedQuery",
        query = "SELECT role.roleName FROM SysRole as role WHERE role.roleCode = ?1 order by role.id DESC")
})
@org.hibernate.annotations.NamedQueries({
        @org.hibernate.annotations.NamedQuery(
                name = "SysRole.selectRoleForNamedQuery",
                query = "SELECT role FROM SysRole as role WHERE role.roleCode = ?1 order by role.id DESC")
})
@NamedNativeQueries({
        @NamedNativeQuery(
                name = "SysRole.selectRoleForNamedNativeQuery",
                query = "SELECT update_by , COUNT(update_by) AS update_by_count FROM sys_role as role WHERE role.role_code = ?1 GROUP BY update_by",
                resultSetMapping = "RoleDtoResultMapping"
        )
})
@SqlResultSetMappings({
        @SqlResultSetMapping(
                name = "RoleDtoResultMapping",
                classes = {@ConstructorResult(
                        targetClass = RoleDto.class,
                        columns = {
                                @ColumnResult(name = "update_by",type = String.class),
                                @ColumnResult(name = "update_by_count",type = Long.class)
                        })
                }
        )
})
public class SysRole {

    /**
    * 主键id
    */
    @Id
    private String id;

    /**
    * 角色名称
    */
    private String roleName;

    /**
    * 角色编码
    */
    private String roleCode;

    /**
    * 描述
    */
    private String description;

    /**
    * 创建人
    */
    private String createBy;

    /**
    * 创建时间
    */
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @Temporal(TemporalType.TIMESTAMP)
    private Date createTime;

    /**
    * 更新人
    */
    private String updateBy;

    /**
    * 更新时间
    */
    private Date updateTime;

}
